import Mock, { Random } from 'mockjs';

const list: any = [];
for (let i = 0; i < 20; i++) {
  list.push({
    id: Random.id(),
    name: Random.cname(),
    code: Random.integer(10000000, 80000000),
    url: Random.url('http'),
    createTime: Random.datetime('yyyy-MM-dd HH:mm'),
    updateTime: Random.datetime('yyyy-MM-dd HH:mm'),
    address: Random.county(true),
    phone: Random.integer(13000000000, 19900000000),
    sex: Random.natural(1, 2),
    age: Random.natural(18, 40),
    status: Random.boolean()
  })
}
const menus = [{
  id: 1,
  name: '权限管理',
  css: 'menu',
  type: 1,
  path:'/auth',
  menuId: 1001,
  createTime: '2020-10-20',
  weight: 1,
  menu: false,
  childs: [{
    id: 10,
    path: '/user',
    name: '用户管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1002,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },{
    id: 10,
    path: '/department',
    name: '部门管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1002,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },
  {
    id: 11,
    path: '/role',
    name: '角色管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },{
    id: 11,
    path: '/permission',
    name: '权限设置',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  }]
},{
  id: 1,
  name: '应用管理',
  css: 'menu',
  type: 1,
  path:'/auth',
  menuId: 1001,
  createTime: '2020-10-20',
  weight: 1,
  menu: false,
  childs: [{
    id: 10,
    path: '/user',
    name: '菜单管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1002,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },
  {
    id: 11,
    path: '/role',
    name: '设置管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },
  {
    id: 11,
    path: '/role',
    name: '字典管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  }]
},{
  id: 1,
  name: '日志管理',
  css: 'menu',
  type: 1,
  path:'/auth',
  menuId: 1001,
  createTime: '2020-10-20',
  weight: 1,
  menu: false,
  childs: [{
    id: 10,
    path: '/user',
    name: '操作日志',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1002,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },
  {
    id: 11,
    path: '/role',
    name: '登录日志',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  }]
},{
  id: 1,
  name: '资源管理',
  css: 'menu',
  type: 1,
  path:'/auth',
  menuId: 1001,
  createTime: '2020-10-20',
  weight: 1,
  menu: false,
  childs: [{
    id: 10,
    path: '/user',
    name: '图片管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1002,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },
  {
    id: 11,
    path: '/role',
    name: '文档管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },{
    id: 11,
    path: '/role',
    name: '协议管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  }]
},
{
  id: 1,
  name: '消息管理',
  css: 'menu',
  type: 1,
  path:'/auth',
  menuId: 1001,
  createTime: '2020-10-20',
  weight: 1,
  menu: false,
  childs: [{
    id: 10,
    path: '/user',
    name: '通知&公告',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1002,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  },
  {
    id: 11,
    path: '/role',
    name: '客服管理',
    css: 'menu',
    open: false,
    type: 1,
    menuId: 1003,
    menu: true,
    createTime: '2020-10-20',
    weight: 1
  }]
}];
/**
 * 获取省市区三级联动
 */
const options: any = [];
for (let i = 0; i < 20; i++) {
  options.push({
    id: Random.id(),
    value: Random.province(),
    label: Random.province(),
    children: []
  })
  for (let j = 0; j < 5; j++) {
    options[i].children.push({
      id: Random.id(),
      value: Random.city(),
      label: Random.city(),
      children: []
    })
    for (let k = 0; k < 5; k++) {
      options[i].children[j].children.push({
        id: Random.id(),
        value: Random.county(),
        label: Random.county()
      })
    }
  }
}
const rolelist: any = [];
for (let i = 0; i < 20; i++) {
  rolelist.push({
    id: Random.id(),
    name: Random.first(),
    code: Random.integer(1, 200),
    createTime: Random.datetime('yyyy-MM-dd HH:mm'),
    updateTime: Random.datetime('yyyy-MM-dd HH:mm')
  })
}
const authoritis: any = [];
for (let i = 0; i < 20; i++) {
  authoritis.push({
    id: Random.id(),
    name: Random.first(),
    code: Random.protocol(),
    createTime: Random.datetime('yyyy-MM-dd HH:mm'),
    updateTime: Random.datetime('yyyy-MM-dd HH:mm')
  })
}
  import jsmd5 from 'js-md5'
export default {
  login: (res: any) => {
      let data = JSON.parse(res.body);
      if(data.username=="admin"&&data.password==jsmd5('123456')){
        return {
          code: 0,
          data: {
            additionalInformation: {},
            expire: 2,
            refreshToken: Random.integer(10000000, 80000000),
            scope: [""],
            token: Random.integer(10000000, 80000000),
            appId:Random.integer(10000000, 80000000),
            appKey:Random.integer(10000000, 80000000),
            tokenType: "bearer",
            user:{
              avatar: 'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1111326284,1947690094&fm=11&gp=0.jpg',
              username:Random.cname()
            }
        },
          msg: 'success'
        }
      }else{
        return {
          code: 1,
          data: '暂无用户',
          msg: 'error'
        }
      }

  },
  /**
   * 获取用户列表
   */
  getList: () => {
    return {
      code: 0,
      data: {
        total: list.length,
        list: list
      },
      msg: 'success'
    }
  },
  /**
   * 创建用户
   */
  create: (res: any) => {
    let data = JSON.parse(res.body);
    data.id = Random.id();
    data.createTime = Random.datetime('yyyy-MM-dd HH:mm');
    return {
      code: 0,
      data: data,
      msg: 'success'
    }
  },
  /**
   * 更新用户
   */
  update: () => ({
    code: 0,
    msg: 'success'
  }),
  /**
   * 获取列表
   */
  getMenuList: (res: any) => {
    let data = JSON.parse(res.body);
    let obj: (number | string | object)[] = []; // 允许数值和字符串类型
    menus.forEach(element => {
      if (element.type == data.type) {
        obj.push(element)
      }
    });
    return {
      code: 0,
      data: obj,
      msg: 'success'
    }
  },
  /**
   * 获取省市级三级联动
   */
  getAddressList: () => {
    return {
      code: 0,
      data: options,
      msg: 'success'
    }
  },
  /**
   * 获取省市级三级联动
   */
  getRoleList: () => {
    return {
      code: 0,
      data: rolelist,
      msg: 'success'
    }
  },
  getAuthoritis: () => {
    return {
      code: 0,
      data: authoritis,
      msg: 'success'
    }
  }

}
